import {getCommonParamInfo} from "@/utils/commonParamUtil";


export function getFromAssociationFlag(ruleInfo, commonParam) {
    if (!ruleInfo) {
        return true;
    }
    let ruleInfoList = ruleInfo.ruleInfoList
    let resFlag = false;
    let beforeOperate = "or";
    // 从上到下一次执行
    for (let index = 0; index < ruleInfoList.length; index++) {
        // 如果当前为子表格，则递归调用
        let info = ruleInfoList[index];
        if (info.isSon) {
            let res = getFromAssociationFlag(info.ruleInfo, commonParam);
            resFlag = doOperate(resFlag, res, beforeOperate);
        } else {
            // 获取变量值
            let paramInfo = getCommonParamInfo(commonParam, info.variableName);
            // 解析当前行的结果
            let currentFlag = doCurrentFlag(paramInfo, info.ruleType, info.value);
            resFlag = doOperate(resFlag, currentFlag, beforeOperate);
        }
        beforeOperate = info.linkType;
    }
    return resFlag;
}

function doOperate(value1, value2, operateType) {
    if (operateType === "or") {
        return value1 || value2
    } else {
        return value1 && value2
    }
}

function doCurrentFlag(sourceValue, ruleType, value) {
    if (typeof sourceValue !== "string") {
        sourceValue = "" + sourceValue;
    }
    if (typeof value !== "string") {
        value = "" + value;
    }
    switch (ruleType) {
        case "eq":
            return sourceValue === value;
        case "neq":
            return sourceValue !== value;
        case "bg":
            return sourceValue > value;
        case "bgeq":
            return sourceValue >= value;
        case "lg":
            return sourceValue < value;
        case "lgeq":
            return sourceValue >= value;
        case "like":
            return sourceValue.includes(value);
        case "noLike":
            return !sourceValue.includes(value);
        case "null":
            return sourceValue === null || sourceValue === '' || sourceValue === 'null' || sourceValue === undefined;
        case "nonNull":
            return sourceValue !== null && sourceValue !== 'null' && sourceValue !== '' && sourceValue !== undefined  && sourceValue !== 'undefined';
        case "isTrue":
            return sourceValue === true || sourceValue === "true";
        case "isFalse":
            return sourceValue === false || sourceValue === "false";
    }
}